rm(list=ls(all=TRUE))

path <- "" #here you need to adjust the path macro to the place on your computer
setwd(path)

library(haven)
library(dplyr)
library(ggplot2)
library(Hmisc)
library(ggrepel)
library(xlsx)
library(cowplot)

# the original data file will be freely available online (http://www.poldem.eu/). For now, please ask for the latest version from the authors.

dat <- read_dta(paste0(path, "\\data_analyze_14072017.dta")) 

dat$iso2code <- as_factor(dat$iso2code)

dat2 <- dat %>%
  select(-wparties) %>% 
  filter(country!=31) %>%
  mutate(wparties = ifelse(parties==1, aglpopw_event, 0)) %>% 
  mutate(sponsored = ifelse(parties==1 | unions_private==1 | 
                            unions_public==1 | unions_both==1 |
                            unions_unknown==1 | otherorg==1 |
                            socialgroups_occup==1, 1, 0)) %>% 
  mutate(wsponsored = ifelse(sponsored==1, aglpopw_event, 0)) %>% 
  select(iso2code, wparties, wsponsored, aglpopw_event, aglpopw_part_all, region) %>%
  group_by(region, iso2code) %>% 
  mutate(all_party = sum(wparties),
         all_events = sum(aglpopw_event),
         all_participants = sum(aglpopw_part_all),
         all_sponsored = sum(wsponsored)) %>% 
  select(region, iso2code, all_party, all_events, all_participants, all_sponsored) %>% 
  unique(.) %>% 
  ungroup(.) %>% 
  mutate(share_from_all=all_party*100/all_events,
         share_from_sponsored=all_party*100/all_sponsored) %>% 
  mutate(diff_threshold=median(share_from_sponsored),
         diff_threshold_avg=mean(share_from_sponsored))


# adjusting the sample, dropping parties which have not sponsored 
# protests and cabinets under which no protest has happened.

table(dat$country)
length(unique(dat$country))

diff_cntry <- dat2$iso2code[dat2$share_from_sponsored>=dat2$diff_threshold]
nodiff_cntry <- dat2$iso2code[dat2$share_from_sponsored<dat2$diff_threshold]

avg_diff_cntry <- dat2$iso2code[dat2$share_from_sponsored>=dat2$diff_threshold_avg]
avg_nodiff_cntry <- dat2$iso2code[dat2$share_from_sponsored<dat2$diff_threshold_avg]

######################
###### Figure 2 ######
######################

ggplot(dat2, aes(x=all_events, y=share_from_sponsored, label=iso2code)) +
  geom_point(aes(shape=factor(region)), show.legend = FALSE) +
  geom_text_repel() +
  geom_smooth(method="loess", color="black", se=FALSE, size=0.4) +
  xlab("Total number of protest events") +
  ylab("Share of party protests") +
  theme_bw() 

ggsave(paste0(path, "\\scatter_event_cntry.eps"),
       dpi=300, scale=0.9, width=7, height=7)

ggsave(paste0(path, "\\scatter_event_cntry.png"),
       dpi=300, scale=0.9, width=7, height=7)

###################################
###### Appendix E - Figure 2 ######
###################################

ggplot(dat2, aes(x=all_participants, y=share_from_sponsored, label=iso2code)) +
  geom_point(aes(shape=factor(region)), show.legend = FALSE) +
  geom_text_repel() +
  geom_smooth(method="loess", color="black", se=FALSE, size=0.4, span=0.84) +
  xlab("Total number of protest participants") +
  ylab("Share of party protests") +
  scale_x_continuous(labels = function(x) format(x, scientific = FALSE)) +
  theme_bw() 

ggsave(paste0(path, "\\scatter_participants_cntry.eps"),
       dpi=300, scale=0.9, width=7, height=7)

ggsave(paste0(path, "\\scatter_participants_cntry.png"),
       dpi=300, scale=0.9, width=7, height=7)


################################################
###### Appendix C figure 1 and figure 2 ########
################################################

# low and high contexts

ggplot(dat2, aes(x=reorder(iso2code, -share_from_sponsored), y=share_from_sponsored)) +
  geom_bar(stat = "identity") +
  geom_hline(yintercept = unique(dat2$diff_threshold), linetype="dashed") +
  geom_text(aes(25.7,12.47,label = paste0("Differentiation Threshold (",
                                          round(unique(dat2$diff_threshold),2),
                                          "%)"), vjust = 1.5)) +
  xlab("") +
  ylab("Share of party protests") +
  theme_bw()


ggsave(paste0(path, "\\median_share_cntry.eps"),
       dpi=300, scale=0.8, width=10, height=7)
ggsave(paste0(path, "\\median_share_cntry.png"),
       dpi=300, scale=0.8, width=10, height=7)

ggplot(dat2, aes(x=reorder(iso2code, -share_from_sponsored), y=share_from_sponsored)) +
  geom_bar(stat = "identity") +
  geom_hline(yintercept = unique(dat2$diff_threshold_avg), linetype="dashed") +
  geom_text(aes(25.7,12.47,label = paste0("Differentiation Threshold (",
                                          round(unique(dat2$diff_threshold_avg),2),
                                          "%)"), vjust = 1.5)) +
  xlab("") +
  ylab("Share of party protests") +
  theme_bw()


ggsave(paste0(path, "\\avg_share_cntry.eps"),
       dpi=300, scale=0.8, width=10, height=7)
ggsave(paste0(path, "\\avg_share_cntry.png"),
       dpi=300, scale=0.8, width=10, height=7)
